/*================================================================
*   Copyright (C) 2024 baichao All rights reserved.
*
*   文件名称：fork2.cpp
*   创 建 者：baichao
*   创建日期：2024年03月29日
*   描    述：
*
================================================================*/

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <wait.h>
int g_int = 1;
int main() {
  int local_int = 1;
  int *malloc_int = (int*)malloc(sizeof(int));
  *malloc_int = 1;
  pid_t pid = fork();
  if (pid == 0) /*子进程*/
  {
    local_int = 0;
    g_int = 0;
    *malloc_int = 0;
    fprintf(stderr, "[CHILD ] child change local global malloc value to 0\n");
    free(malloc_int);
    sleep(10);
    fprintf(stderr, "[CHILD ] child exit\n");
    exit(0);
  } else if (pid < 0) {
    printf("fork failed (%s)", strerror(errno));
    return 1;
  }
  fprintf(stderr, "[PARENT] wait child exit\n");
  waitpid(pid, NULL, 0);
  fprintf(stderr, "[PARENT] child have exit\n");
  printf("[PARENT] g_int = %d\n", g_int);
  printf("[PARENT] local_int = %d\n", local_int);
  printf("[PARENT] malloc_int = %d\n", local_int);
  free(malloc_int);
  return 0;
}